package com.ylg.spring.core;

public class AnnotationApplicationContext implements ApplicationContext {


    private BeanFactory beanFactory;

    /**
     * 初始化Spring容器
     * @param configs
     */
    public AnnotationApplicationContext(Class... configs) {

        // 创建bean工厂
        this();

        // 注册BeanDefinition 用于存储bean的定义信息 比如id,class等信息
        registry(configs);

        // 刷新容器 真正的创建对象
        refresh();

    }

    private void refresh() {
        this.beanFactory.refreshBeanMap();
    }

    private void registry(Class[] configs) {
        this.beanFactory.registryBeanDefinition(configs);
    }

    public AnnotationApplicationContext() {
        this.beanFactory = new DefaultBeanFactory();
    }

    @Override
    public Object getBean(String beanName) {

        return beanFactory.getSingleObjectBeanMap().get(beanName);
    }

    @Override
    public <T> T getBean(String beanName, Class<T> beanClass) {
        return (T) beanFactory.getSingleObjectBeanMap().get(beanName);
    }
}
